const app = getApp()

Page({
  data: {
    //分页设置
    totalPage: 1,
    page: 1,
    videoList: [],
    screenWidth: 350,
    serverUrl: "",
    searchValue: ""
  },
  onLoad: function (params) {
    var me = this;
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    var searchValue = params.searchValue;
    me.setData({
      screenWidth: screenWidth,
      searchValue: searchValue
    });

    var isSavedRecord = params.isSavedRecord;
    if (isSavedRecord == null || isSavedRecord == '' || isSavedRecord == undefined) {
      isSavedRecord = 0;
    }
    //获取当前的分页数
    var page = me.data.page;
    this.getAllVideos(page, isSavedRecord);
  },
  //分页查询请求的封装
  getAllVideos: function (page, isSavedRecord) {
    var me = this;
    var serverUrl = app.serverUrl;
    wx.showToast({
        title: '请等待,加载中...',
      }),
      wx.request({
        url: serverUrl + "/video/showAll?page=" + page + '&isSavedRecord=' + isSavedRecord,
        data: {
          videoDesc: me.data.searchValue
        },
        method: "POST",
        success: function (res) {
          wx.hideLoading();
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
          //如果page为第一页，设置videoList为空
          if (page == 1) {
            me.setData({
              videoList: []
            })
          }
          var videoList = res.data.data.rows;
          var newVideoList = me.data.videoList;
          me.setData({
            videoList: newVideoList.concat(videoList),
            page: page,
            totalPage: res.data.data.total,
            serverUrl: serverUrl
          })
        }
      })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.getAllVideos(1, 0);
    wx.showNavigationBarLoading();
  },
  //上拉查询更多
  onReachBottom: function () {
    var me = this;
    var currentPage = me.data.page;
    var totalPage = me.data.totalPage;

    //判断是否到了最后一页
    if (totalPage == currentPage) {
      wx.showToast({
        title: '没有啦，别刷了~',
        icon: "none"
      })
      return;
    };
    var page = currentPage + 1;
    me.getAllVideos(page, 0);
  },
  showVideoInfo: function (e) {
    var me = this;
    var videoList = me.data.videoList;
    var arrIndex = e.target.dataset.arrindex;
    var videoInfo = JSON.stringify(videoList[arrIndex]);
    wx.redirectTo({
      url: '../videoInfo/videoInfo?videoInfo='+videoInfo,
    })

  }

})